﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace DienLib.PopUp
{
    /// <summary>
    /// Add DisplaySelectionPath & FormatSelectionBox(). Don't forget TextSearch.Text & IsTextSearchEnabled
    /// To Use : Search by TextSearch.Text Property, but when Press Enter or Tab (or call FormatSelectionBox) will appear DisplaySelectionPath Property
    /// </summary>
    public class ComboBoxD : System.Windows.Controls.ComboBox
    {
        string displayProp = null;
        int formatedIndex = -1;
        bool isFormated = false;

        public ComboBoxD()
        {
            // MORE CODE HERE
        }

        public string DisplaySelectionPath
        {
            set
            {
                displayProp = value;
            }
        }

        protected override void OnDropDownOpened(EventArgs e)
        {
            base.OnDropDownOpened(e);
            if (isFormated) ReloadSelectionBox();
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            base.OnTextInput(e);
            if (isFormated & e.Text != "\r")
            {
                    isFormated = false; formatedIndex = -1;
            }
        }

        protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.Key == Key.Enter | e.Key == Key.Tab)
            {
                if (!this.IsDropDownOpen)
                    if (!isFormated) FormatSelectionBox();
            }
            else if (e.Key == Key.Down | e.Key == Key.Up)
            {
                if (!this.IsDropDownOpen)
                    if (isFormated) ReloadSelectionBox();
            }
            else if (e.Key == Key.Escape)
            {
                if (!this.IsDropDownOpen)
                    ReloadSelectionBox();
            }
        }


        // Khi Enter ma khong trong selection thi giu nguyen khong Format
        // Khi Drop Down ma khong trong Selection thi giu nguyen, khong phuc hoi BK
        // Khi Move Button OK ma khong trong Selection thi giu nguyen khong FOrmat
        // Khi Out Button OK ma khong trong Selection thi khong phuc hoi

        // Khi Enter ma trong Selection thi Format => Sau khi Format thi Out Selection => Nhung van phai phuc hoi.

        // Khi Enter | Tab | In ButOK => Format : 
        //1. Khong thuoc List => Khong Format | BackupIndex = -1
        //2. Thuoc List => Formatted roi BackupIndex & changed = true
        // Khi DropDown | Out ButOK => Reload
        //1. Neu Formatted thi phuc hoi
        //2. Neu khong Format thi thoi
        // Khi Escape => Reload

        public void FormatSelectionBox() // Khi mouse_enter OK button hoac Enter or Tab trong comboBox => 
        {
            if (isFormated) return; // Already Formated, need Reload first before ReFormat again

            object item = this.SelectedItem; // If Selection not in List then return
            if (item == null) { isFormated = false; return; }

            formatedIndex = this.SelectedIndex; isFormated = true;
            string txt = string.Empty;
            try { txt = (string)item.GetType().GetProperty(displayProp).GetValue(item, null); }
            catch (Exception e) { this.Text = "Set DisplaySelectionPath First!"; }
            this.Text = txt;
        }

        private void ReloadSelectionBox()
        {
            if (isFormated) // Make sure changed by User will not highlighted
            {
                this.SelectedIndex = formatedIndex; // How to rehighlighted ???
                isFormated = false; formatedIndex = -1;
            }
        }
    }
}
